package com.gxar.navigation.server.base.reference;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxar.navigation.server.base.common.constant.QiMenConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/9/21 10:02
 */
@Slf4j
@Service
public class QiMenReference {

    /**
     * 活动预览
     *
     * @param activeId 活动ID
     * @return 预览结果
     */
    public JSONObject previewActivity(String activeId) {
        // 构建范化调用服务
        GenericService genericService = this.buildGenericService();
        //封装参数(params为Map<String, Object>，参数名和值)
        Map<String, String> params = new HashMap<>();
        Map<String, String> paramJson = new HashMap<>();
        paramJson.put("service", QiMenConstant.PREVIEW_ACTIVITY);
        paramJson.put("active_id", activeId);
        params.put("params", JSON.toJSONString(paramJson));
        String[] paramTypes = new String[params.size()];
        Object[] paramValues = new Object[params.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            paramTypes[i] = entry.getValue().getClass().getTypeName();
            paramValues[i] = entry.getValue();
            i++;
        }
        Object userServiceObject = genericService.$invoke("dubboRpc", paramTypes, paramValues);
        if (Objects.nonNull(userServiceObject)) {
            return JSON.parseObject(JSON.toJSONString(userServiceObject));
        }
        return null;
    }

    /**
     * 活动发布
     *
     * @param activeId 活动id
     * @return 发布结果
     */
    public JSONObject publishActivity(String activeId) {
        // 构建范化调用服务
        GenericService genericService = this.buildGenericService();
        //封装参数(params为Map<String, Object>，参数名和值)
        Map<String, String> params = new HashMap<>();
        Map<String, String> paramJson = new HashMap<>();
        paramJson.put("service", QiMenConstant.PUBLISH_ACTIVITY);
        paramJson.put("active_id", activeId);
        params.put("params", JSON.toJSONString(paramJson));
        String[] paramTypes = new String[params.size()];
        Object[] paramValues = new Object[params.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            paramTypes[i] = entry.getValue().getClass().getTypeName();
            paramValues[i] = entry.getValue();
            i++;
        }
        Object userServiceObject = genericService.$invoke("dubboRpc", paramTypes, paramValues);
        if (Objects.nonNull(userServiceObject)) {
            return JSON.parseObject(JSON.toJSONString(userServiceObject));
        }
        return null;
    }

    /**
     * 根据活动id查询活动信息
     *
     * @param activeId 活动id
     * @return 活动信息带用户id
     */
    public JSONObject activityInfo(String activeId) {
        // 构建范化调用服务
        GenericService genericService = this.buildGenericService();
        //封装参数(params为Map<String, Object>，参数名和值)
        Map<String, String> params = new HashMap<>();
        Map<String, String> paramJson = new HashMap<>();
        paramJson.put("service", QiMenConstant.ACTIVITY_INFO);
        paramJson.put("active_id", activeId);
        params.put("params", JSON.toJSONString(paramJson));
        String[] paramTypes = new String[params.size()];
        Object[] paramValues = new Object[params.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            paramTypes[i] = entry.getValue().getClass().getTypeName();
            paramValues[i] = entry.getValue();
            i++;
        }
        Object userServiceObject = genericService.$invoke("dubboRpc", paramTypes, paramValues);
        if (Objects.nonNull(userServiceObject)) {
            return JSON.parseObject(JSON.toJSONString(userServiceObject));
        }
        return null;
    }

    /**
     * 根据accessToken获取登陆的用户信息
     *
     * @param accessToken accessToken
     * @return 用户信息及登陆状态
     */
    public JSONObject userInfo(String accessToken) {
        // 构建范化调用服务
        GenericService genericService = this.buildGenericService();
        //封装参数(params为Map<String, Object>，参数名和值)
        Map<String, String> params = new HashMap<>();
        Map<String, String> paramJson = new HashMap<>();
        paramJson.put("service", QiMenConstant.USER_INFO);
        paramJson.put("active_id", accessToken);
        params.put("params", JSON.toJSONString(paramJson));
        String[] paramTypes = new String[params.size()];
        Object[] paramValues = new Object[params.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            paramTypes[i] = entry.getValue().getClass().getTypeName();
            paramValues[i] = entry.getValue();
            i++;
        }
        Object userServiceObject = genericService.$invoke("dubboRpc", paramTypes, paramValues);
        if (Objects.nonNull(userServiceObject)) {
            return JSON.parseObject(JSON.toJSONString(userServiceObject));
        }
        return null;
    }


    /**
     * 构建范化调用服务
     *
     * @return 范化调用服务
     */
    private GenericService buildGenericService() {
        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        // 弱类型接口名
        reference.setApplication(new ApplicationConfig("user-provider"));
        //接口类全限定类名
        reference.setInterface("com.gxar.api.UserService");
        reference.setCheck(false);
        //接口版本号
        reference.setVersion("1.0.0");
        // 声明为泛化接口
        reference.setGeneric(true);
        reference.setSerialization("fastjson");
        // 用org.apache.dubbo.rpc.service.GenericService可以替代所有接口引用
        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        return cache.get(reference);
    }
}
